package org.acghub.mtdb.core;

import com.alibaba.fastjson2.JSONObject;
import org.acghub.mtdb.common.exception.MarshalException;
import org.bson.Document;

/**
 * @author zhouhe
 */
public interface Marshal {
    default Document marshalDocument() {
        return marshal(new Document());
    }

    default JSONObject marshalJson() {
        return marshal(new JSONObject());
    }

    default Object marshal(Object writer) {
        if (writer instanceof Document) {
            return marshal((Document) writer);
        } else if (writer instanceof JSONObject) {
            return marshal((JSONObject) writer);
        }
        throw new MarshalException("unknown writer type: " + writer.getClass().getName());
    }

    default void unmarshal(Object reader) throws MarshalException {
        if (reader instanceof Document) {
            unmarshal((Document) reader);
        } else if (reader instanceof JSONObject) {
            unmarshal((JSONObject) reader);
        }
        throw new MarshalException("unknown reader type: " + reader.getClass().getName());
    }

    //document
    Document marshal(Document writer);

    void unmarshal(Document reader);

    // fastjson2
    JSONObject marshal(JSONObject writer);

    void unmarshal(JSONObject reader);


}
